﻿using System;
using System.Reflection;

namespace Jack.RedisEventBus.Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1();
        }

        static void Test1()
        {
            Type eventHandlerType = typeof(OrderEventHandler);
            bool isAssignableTo = IsAssignableTo<IEventHandle>(eventHandlerType);
            if (isAssignableTo == false) return;
            //var interfaces = eventHandlerType.GetInterfaces();
            var @interface = eventHandlerType.GetInterface("IEventHandle`1");
            var eventType = @interface.GetGenericArguments()[0];
            string eventName = eventType.FullName;
            if (eventType.IsDefined(typeof(EventNameAttribute), false))
            {
                eventName = eventType.GetCustomAttribute<EventNameAttribute>().Name;
            }
            Console.WriteLine(eventName);
        }

        static bool IsAssignableTo<T>(Type @this)
        {
            if (@this == null)
            {
                throw new ArgumentNullException("this");
            }

            return typeof(T).IsAssignableFrom(@this);
        }
    }
}
